package cn.oveay.classdesign.controller.servlet;

import cn.oveay.classdesign.bean.basebean.Subject;
import cn.oveay.classdesign.service.SubjectService;
import cn.oveay.classdesign.service.impl.SubjectServiceImpl;
import cn.oveay.classdesign.util.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet("/list")
public class ListServlet extends BaseServlet {
    private SubjectService subjectService = new SubjectServiceImpl();
    public String forward(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Subject> list = subjectService.findAll();
        req.setAttribute("subjectList", list);
        req.setAttribute("currdate", new Date().getTime());
        return "/WEB-INF/pages/list.jsp";
    }

    public String find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String key = req.getParameter("keywords");
        if(key == null || key.trim().equals("")){
            return "r:/list?method=forward";
        }
        List<Subject> list = subjectService.findByKey(key);
        req.setAttribute("subjectList", list);
        req.setAttribute("currdate", new Date().getTime());
        req.setAttribute("keywords", key);
        return "/WEB-INF/pages/list.jsp";
    }
}
